﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Utils;
using Utils.DatabaseUtils;
using Utils.DC.Web.UI;

namespace DCSLibrary
{
    public class DocumentUI
    {
        private const String CACHE_KEY = "Document_GetAll";

        public int Add(int CategoryPageID, String DocumentName, String DocumentAlias, String DocumentImage,
            String DocumentPath, String MetaKeyword, String MetaDescription, String DocumentType, decimal DocumentSize, bool IsHot, bool IsTypical,
            bool IsNew, int ViewTime, int DownloadTime, int ViewOrder)
        {
            try
            {
                int i = SqlHelper.ExecuteNonQuery(Common.Conn, "Document_add", CategoryPageID, DocumentName, DocumentAlias, DocumentImage,
            DocumentPath, MetaKeyword, MetaDescription,  DocumentType, DocumentSize, IsHot, IsTypical,IsNew,  ViewTime, DownloadTime, ViewOrder);
                if (i > 0)
                {
                    WebUtility.RemoveCache(CACHE_KEY);
                }

                return i;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }

        public int Update(int DocumentID, int CategoryPageID, String DocumentName, String DocumentAlias, String DocumentImage,
            String DocumentPath, String MetaKeyword, String MetaDescription, String DocumentType, decimal DocumentSize, 
            bool IsHot, bool IsTypical,bool IsNew, int ViewTime, int DownloadTime, int ViewOrder)
        {
            try
            {
                int i = SqlHelper.ExecuteNonQuery(Common.Conn, "Document_update", DocumentID, CategoryPageID, DocumentName, DocumentAlias, DocumentImage,
            DocumentPath, MetaKeyword, MetaDescription, DocumentType, DocumentSize, IsHot, IsTypical,IsNew, ViewTime, DownloadTime, ViewOrder);

                if (i > 0)
                {
                    WebUtility.RemoveCache(CACHE_KEY + "_" + DocumentID);
                    WebUtility.RemoveCache(CACHE_KEY);
                }
                return i;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }

        public DataSet GetAll()
        {
            try
            {
                object obj = WebUtility.GetCacheObject(CACHE_KEY);
                if (obj == null)
                {
                    DataSet ds = SqlHelper.ExecuteDataset(Common.Conn, CommandType.StoredProcedure, "Document_getall");
                    if (ds != null)
                    {
                        WebUtility.InsertCache(CACHE_KEY, ds);
                    }
                    return ds;
                }
                else
                {
                    return (DataSet)obj;
                }
            }
            catch (Exception e)
            {
                return null;
            }
        }

        public DataSet GetByID(int DocumentID)
        {
            try
            {
                object obj = WebUtility.GetCacheObject(CACHE_KEY + "_" + DocumentID.ToString());
                if (obj == null)
                {
                    DataSet ds = SqlHelper.ExecuteDataset(Common.Conn, "Document_get", DocumentID);
                    if (ds != null)
                    {
                        WebUtility.InsertCache(CACHE_KEY + "_" + DocumentID.ToString(), ds);
                    }
                    return ds;
                }
                else
                {
                    return (DataSet)obj;
                }
            }
            catch (Exception e)
            {
                return null;
            }

        }

        public int DeleteByID(int DocumentID)
        {
            try
            {
                int i = SqlHelper.ExecuteNonQuery(Common.Conn, "Document_delete", DocumentID);
                if (i > 0)
                {
                    WebUtility.RemoveCache(CACHE_KEY);
                }
                return i;
            }
            catch (Exception e)
            {
                return -1;
            }
        }
    }
}
